<?php
namespace daicuo;

class Category
{
    // 错误信息
    protected static $error = 'error';
    
    /**
     * 获取错误信息
     * @return mixed
     */
    public function getError()
    {
        return self::$error;
    }
    
    /**
     * 默认字段
     * @param array $data 初始数据 
     * @return array 表单与表格定义的格式
     */
    public function fields($data){
        
        //表单扩展字段自动处理
        $fields = DcFields(config('custom_fields.term_meta'), $data);
        foreach($fields as $key=>$value){
            if( isset($value['filter']) ){
                if(!in_array($value['filter'],['common','category'])){
                    unset($fields[$key]);
                }
            }
        }
        
        //表单初始化字段
        $fields = DcArrayArgs($fields, [
            'html_1' => [
                'type'            => 'html',
                'value'           => '<div class="row"><div class="col-12 col-md-8">',
                'order'           => 0,
            ],
            'term_id' => [
                'type'            => 'hidden',
                'value'           => $data['term_id'],
                'order'           => 1,
                'data-filter'     => false,
                'data-visible'    => true,
                'data-width'      => '80',
                'data-width-unit' => 'px',
                'data-sortable'   => true,
                'data-sort-name'  => 'term_id',
                'data-order'      => 'asc',
            ],
            'term_parent' => [
                'type'            => 'select',
                'value'           => $data['term_parent'],
                'order'           => 11,
                'option'          => DcTermOption(['module'=>input('get.term_module')]),
                'data-filter'     => true,
                'data-visible'    => false,
                'data-width'      => '100',
            ],
            'term_type' => [
                'type'            => 'hidden',
                'value'           => 'category',
                'order'           => 21,
                'data-filter'     => false,
                'data-visible'    => false,
            ],
            'term_name' => [
                'type'            => 'text',
                'value'           => $data['term_name'],
                'order'           => 31,
                'required'        => true,
                'data-filter'     => false,
                'data-visible'    => true,
                'data-align'      => 'left',
            ],
            'term_slug' => [
                'type'            => 'text',
                'value'           => $data['term_slug'],
                'order'           => 41,
                'data-filter'     => false,
                'data-visible'    => true,
                'data-align'      => 'left',
            ],
            'term_status' => [
                'type'            => 'select',
                'value'           => DcEmpty($data['term_status'],'normal'),
                'option'          => ['normal'=>lang('normal'),'hidden'=>lang('hidden')],
                'order'           => 51,
                'data-filter'     => true,
                'data-visible'    => false,
            ],
            'term_status_text' => [
                'order'           => 61,
                'data-title'      => lang('term_status'),
                'data-visible'    => true,
                'data-width'      => '100',
            ],
            'term_count' => [
                'order'           => 71,
                'data-visible'    => true,
                'data-sortable'   => true,
                'data-width'      => '100',
            ],
            'term_info' => [
                'type'            => 'textarea',
                'value'           => $data['term_info'],
                'rows'            => 5,
                'order'           => 101,
                'data-filter'     => false,
                'data-visible'    => false,
            ],
            'html_2'      => [
                'type'            => 'html',
                'value'           => '</div><div class="col-12 col-md-4">',
                'order'           => 199,
            ],
            'term_order' => [
                'type'            => 'number',
                'value'           => intval($data['term_order']),
                'order'           => 201,
                'data-filter'     => true,
                'data-visible'    => true,
                'data-width'      => '100',
                'data-sortable'   => true,
            ],
            'term_module' => [
                'type'            => 'text',
                'value'           => DcEmpty($data['term_module'],'common'),
                'order'           => 211,
                'data-filter'     => true,
                'data-visible'    => true,
                'data-width'      => '100',
                'data-formatter'  => 'daicuo.admin.table.formatter',
            ],
            'term_controll' => [
                'type'            => 'text',
                'value'           => DcEmpty($data['term_controll'],'category'),
                'order'           => 221,
                'data-filter'     => false,
                'data-visible'    => true,
                'data-width'      => '100',
                'data-formatter'  => 'daicuo.admin.table.formatter',
            ],
            'term_action' => [
                'type'            => 'text',
                'value'           => DcEmpty($data['term_action'],'index'),
                'order'           => 231,
                'data-filter'     => false,
                'data-visible'    => true,
                'data-width'      => '100',
                'data-formatter'  => 'daicuo.admin.table.formatter',
            ],
            'html_3' => [
              'type'              => 'html',
              'value'             => '</div></div>',
              'order'             => 999,
            ]
        ]);
        
        return list_sort_by($fields, 'order', 'asc', true);
    }

}